# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml

trigger:
- master

variables:
  vmImageName: 'ubuntu-latest'

  environmentName: 'WebsitesLinuxVMEnvironment'
  
  resourceName: 'websites'
  
  rootFolder: $(System.DefaultWorkingDirectory)
  
  websiteDir: '/var/www/clients/client1/web100/web/'

pool:
  vmImage: ubuntu-latest

# steps:
# - script: echo Hello, world!
#   displayName: 'Run a one-line script'
stages:
- stage: Build
  displayName: Build stage
  variables:
    phpVersion: '8.3'
  jobs:
  - job: BuildJob
    pool:
      vmImage: $(vmImageName)
    steps:
    - script: |
        sudo update-alternatives --set php /usr/bin/php$(phpVersion)
        sudo update-alternatives --set phar /usr/bin/phar$(phpVersion)
        sudo update-alternatives --set phpdbg /usr/bin/phpdbg$(phpVersion)
        sudo update-alternatives --set php-cgi /usr/bin/php-cgi$(phpVersion)
        sudo update-alternatives --set phar.phar /usr/bin/phar.phar$(phpVersion)
        php -version
      workingDirectory: $(rootFolder)
      displayName: 'Use PHP version $(phpVersion)'

    - task: ArchiveFiles@2
      displayName: 'Archive files'
      inputs:
        rootFolderOrFile: '$(rootFolder)'
        includeRootFolder: false
        archiveType: zip
        archiveFile: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
        replaceExistingArchive: true

    - publish: $(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip
      displayName: 'Upload package'
      artifact: drop

- stage: Deploy
  displayName: 'Deploy Web App'
  dependsOn: Build
  condition: succeeded()
  jobs:  
  - deployment: VMDeploy
    displayName: Web deploy
    environment:
      name:  $(environmentName)
      resourceType: VirtualMachine
      tags: websites
    strategy:
      runOnce:
        deploy:
          steps:
          - task: ExtractFiles@1
            inputs:
              archiveFilePatterns: $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
              overwriteExistingFiles: true
              destinationFolder: /tmp/$(Build.BuildId)
          - task: Bash@3
            inputs:
              targetType: 'inline'
              workingDirectory: $(websiteDir)
              script: |
                echo 'files to delete:';
                sudo find . -mindepth 1 ! -path './stats' ! -path './stats/*' ! -path './error' ! -path './error/*' ! -name robots.txt ! -name favicon.ico ! -name 'standard_index.html' ! -name 'azure-pipelines.yml' -exec echo {} +;
                echo 'deleting older files';
                sudo find . -mindepth 1 ! -path './stats' ! -path './stats/*' ! -path './error' ! -path './error/*' ! -name robots.txt ! -name favicon.ico ! -name 'standard_index.html' ! -name 'azure-pipelines.yml' -exec rm -rf {} +;
                echo 'moving websites files';
                sudo cp -rf /tmp/$(Build.BuildId)/* $(websiteDir);
                rm -rf /tmp/$(Build.BuildId) $(Pipeline.Workspace)/drop/$(Build.BuildId).zip
                echo 'done';
